package com.abdu.demo.kotlindemo

import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast

class loginActivity : AppCompatActivity() {
    private var context: Context = this
    private lateinit var tv_loginError: TextView
    private lateinit var lv_loginBox: LinearLayout
    private lateinit var et_account: EditText
    private lateinit var et_password: EditText
    private lateinit var cb_accept: CheckBox
    private lateinit var btn_login: Button
    private lateinit var btn_register: ImageView
    private lateinit var btn_help: ImageView

    private var account: String = ""
    private var password: String = ""
    private var acceptState: Boolean = false

    private lateinit var objectAnimator: ObjectAnimator

    private lateinit var dbHelper: DatabaseHelper
    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        initView()
        login()
    }



    private fun initView() {
        tv_loginError = findViewById(R.id.tv_loginError)
        lv_loginBox = findViewById(R.id.lv_LoginBox)
        et_account = findViewById(R.id.et_account)
        et_password = findViewById(R.id.et_password)
        cb_accept = findViewById(R.id.cb_accept)
        btn_login = findViewById(R.id.btn_login)
        btn_register = findViewById(R.id.btn_register)
        btn_help = findViewById(R.id.btn_help)
        btn_help.setOnClickListener{
            showToast("有事靠自己！")
        }
        btn_register.setOnClickListener{
            startActivity(Intent(context,RegisterActivity::class.java))
            finish()
        }
    }

    private fun login() {
        dbHelper = DatabaseHelper(this)
        sharedPreferences = getSharedPreferences("loginPrefs", Context.MODE_PRIVATE)

        val isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false)
        if (isLoggedIn) {
            startActivity(Intent(context, HomeActivity::class.java))
            finish()
            return
        }

        btn_login.setOnClickListener{
            acceptState = cb_accept.isChecked
            account = et_account.text.toString()
            password = et_password.text.toString()
            if (account.length==0 || password.length==0){
                loginError("账号和密码不能为空")
                return@setOnClickListener
            }
            if (!acceptState){
                loginError("请同意协议")
                return@setOnClickListener
            }

            if (dbHelper.checkUser(account, password)) {
                val editor: SharedPreferences.Editor = sharedPreferences.edit()
                editor.putBoolean("isLoggedIn", true)
                editor.putString("account", account)
                editor.apply()
                showToast("登录成功！")
                tv_loginError.text = ""
                startActivity(Intent(context, HomeActivity::class.java))
                finish()

            } else {
                loginError("用户名或密码错误!")
            }
        }
    }

    private fun showToast(text: String){
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
    }

    private fun loginError(msg: String){
        tv_loginError.text = msg
        objectAnimator = ObjectAnimator.ofFloat(tv_loginError,
            "translationX", 0f, 50f, -50f, 0f)
        objectAnimator.duration = 850
        objectAnimator.repeatCount = 0
        objectAnimator.start()

        objectAnimator = ObjectAnimator.ofFloat(lv_loginBox,
            "translationX", 0f, 30f, -30f, 0f)
        objectAnimator.duration = 700
        objectAnimator.start()
    }
}